package com.qlzb.taskmanagement.utils;


import com.qlzb.taskmanagement.vo.quartzVo.ExcelCols;
import com.qlzb.taskmanagement.vo.quartzVo.ExcelRows;
import org.apache.commons.beanutils.PropertyUtils;
import org.jsoup.nodes.Element;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 对象的封装方法
 */
public class CommonBeanUtils {
    //将数据封装成指定对象
    public static Object encapsulation(Element row, Class clas){
        Field[] fields= CommonUtil.getFields(clas);
        Object beanobj= null;
        try {
            beanobj = clas.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Element indexTd = row.select("td").get(1).select("center").first();
        int i = Integer.parseInt(indexTd.text());
        i = i - 1;
        for(Field field:fields){
            String filedName=field.getName();
            int j_id= ParseAnnotation.getOperationColumnId(field);
            if(j_id==0){
                continue;
            }
            Element stationLink = row.select("[id=listForm:list:"+i+":j_id"+j_id+"]").first();
            String filedValue= stationLink.text();
            try {
                PropertyUtils.setProperty(beanobj,filedName,filedValue);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return beanobj;
    }
    //将数据封装成指定对象
    public static Object encapsulation(ExcelRows excelRow, Class clas){
        Field[] fields= CommonUtil.getFields(clas);
        Object beanobj= null;
        try {
            beanobj = clas.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        for(Field field:fields){
            String filedName=field.getName();
            String  name= ParseAnnotation.getOperationColumnName(field);
            if(name==null){
                continue;
            }
            String filedValue=getValueByTitle(excelRow,name);
            try {
                PropertyUtils.setProperty(beanobj,filedName,filedValue);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return beanobj;
    }
    public static String getValueByTitle(ExcelRows excelRow,String title){
        List<ExcelCols> excelCols= excelRow.getColses();
        for(ExcelCols excelCol:  excelCols){
            if(title.equals(excelCol.getTitle())){
                return excelCol.getValue();

            }
        }
        return null;
    }
}
